<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path;
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<title>教师请假-云校通</title>
<jsp:include page="/WEB-INF/page/common/_header.jsp"></jsp:include>
</head>
<body ontouchstart id="xsqj" style="background: #fff;">
	<div class="weui_tab tab-bottom">
		<div class="weui_tab_bd">
			<div class="weui_cells weui_cells_access ltbg borderLeft" style="margin-top:0;">
				<div class="listHead">
					<ul class="clearfix">
						<li class="on">请假审批</li>
						<li>我的请假</li>
					</ul>
				</div>
			</div>
			
			<div class="weui_tab kcfd-list1" id="tab3">
	            <div class="weui_navbar">
	                <div class="weui_navbar_item" id="approvalIngList">
	                    	待审批(<span id="approvalIngCount">0</span>)
	                </div>
	                <div class="weui_navbar_item" id="approvalOkList">
	                    	已审批
	                </div>
	            </div>
	            <div class="weui_tab_bd">
	                
		            <div class="weui_panel weui_tab_bd_item qingjia mt0">
			            <div class="weui_panel_bd" id="approvalIngItem">
			                
			            </div>
			            <div class="dropload-down">
						    <div class="loadData dropload-hasData">点击加载更多数据...</div>
						</div>
		            </div>
			        
		            <div class="weui_panel weui_tab_bd_item qingjia mt0">
			            <div class="weui_panel_bd" id="approvalOkItem">
			                
			            </div>
			            <div class="dropload-down">
						    <div class="loadData dropload-hasData">点击加载更多数据...</div>
						</div>
		            </div>
	            </div>
	        </div>
			
	        <div class="weui_tab kcfd-list1 hide" id="tab4">
	            <div class="weui_navbar">
	                <div class="weui_navbar_item">
	                    	待审批(<span id="ingCount">0</span>)
	                </div>
	                <div class="weui_navbar_item" id="okList">
	                    	已审批
	                </div>
	            </div>
	            <div class="weui_tab_bd">
	                
		            <div class="weui_panel weui_tab_bd_item qingjia mt0">
			            <div class="weui_panel_bd" id="ingItem">
			                
			            </div>
			            <div class="dropload-down">
						    <div class="loadData dropload-hasData">点击加载更多数据...</div>
						</div>
		            </div>
			        
		            <div class="weui_panel weui_tab_bd_item qingjia mt0">
			            <div class="weui_panel_bd" id="okItem">
			                
			            </div>
			            <div class="dropload-down">
						    <div class="loadData dropload-hasData">点击加载更多数据...</div>
						</div>
		            </div>
	            </div>
	        </div>
	       <a href="<%=basePath %>/ls/teacher/leave/publish" style="display: block;">
		       <div class="addBtnnew hide">
					<div class="linea"></div>
					<div class="lineb"></div>
		       </div>
	       </a>
		</div>
	</div>
	
	<script id="apply-table-template" type="text/x-handlebars-template">	
	{{#each rows}}
     <div class="weui_cell moments__post qingjia">
	      <div class="weui_panel_bd">
			  <a href="<%=basePath%>/ls/teacher/leave/detail/{{id}}" style="display:block;">
	          <div class="weui_media_box weui_media_appmsg">
	              <div class="weui_media_bd">
	                  <h4 class="weui_media_title">{{teacherName}} <span class="f-gray f14">{{dateFormat createTime}}</span></h4>
	                  <div class="weui_media_desc f-black" style="-webkit-line-clamp: inherit;">
	                  		<p><i class="icon icon-85 f-blue"></i> 审批人：{{approver}}</p>	                  		
							<p><i class="icon icon-81 f-blue"></i> 请假类型：{{leaveType}}</p>
	                  		<p><i class="icon icon-106 f-blue"></i> 请假时间：{{startTime}}/{{endTime}}</p>
							<p><i class="icon icon-51 f-blue"></i> 请假时长：{{leaveDays}}天</p>
							{{#if backStatus}}
							<p><i class="icon icon-106 f-blue"></i> 销假时间：{{backStartDate}}/{{backEndDate}}</p>
							<p><i class="icon icon-51 f-blue"></i> 销假时长：{{backDays}}天</p>
                            {{/if}}
	                  </div>
	                  <div class="weui_media_fd  tright weui_btn_area">
	               		<div class="zhangtai"><span class="f-blue">{{#statusFormat leaveStatus}}{{/statusFormat}}</span></div>
	                  </div>
	              </div>
	          </div>
			  </a>
	      </div>
      </div>
     {{/each}}
 </script>
 
 <div style="display:none;" id="weui_mask" class="weui_mask weui_mask_visible" style="transform-origin: 0px 0px 0px; opacity: 1; transform: scale(1, 1);"></div>
 <div style="display:none;" id="weui_dialog" class="weui_dialog weui_dialog_visible" style="transform-origin: 0px 0px 0px; opacity: 1; transform: scale(1, 1);">
	<div class="weui_dialog_hd">
		<strong class="weui_dialog_title">请输入内容</strong>
	</div>
	<div class="weui_dialog_bd">
		<textarea class="weui_input weui-prompt-input" style="height: 80px;width: 95%;" rows="10" id="rejectText"></textarea>
	</div>
	<div class="weui_dialog_ft">
		<a href="javascript:;" id="rejectCancel" class="weui_btn_dialog default">取消</a>
		<a href="javascript:;" id="rejectSure" class="weui_btn_dialog primary">确定</a>
	</div>
 </div>
<jsp:include page="/WEB-INF/page/common/_footer.jsp"></jsp:include>
<script>
		var isLoadApprovalOkList = false, isLoadApprovalNoList = false,
			isLoadIngList = false, isLoadOkList = false, isLoadNoList = false;
		var approvalIngPage = 1, approvalIngRows = 10,
			approvalOkPage = 1, approvalOkRows = 10,
			approvalNoPage = 1, approvalNoRows = 10,
			ingPage = 1,ingRows = 10,
			okPage = 1, okRows = 10, 
			noPage = 1, noRows = 10;
		$(function() {
			$('#tab3').tab({
				defaultIndex: 0,
				activeClass: "tab-blue "
			});
			$('#tab4').tab({
				defaultIndex: 0,
				activeClass: "tab-blue "
			});
			$(".listHead li").click(function(){
				var obj = $(this);
				var index = obj.index();
				$(".ltbg-content").eq(index).show().siblings(".ltbg-content").hide();
				obj.addClass("on").siblings().removeClass("on")
				if(index == 0) {
					$("#tab3").show();
					$("#tab4").hide();
					$(".addBtnnew").hide();
				}
				else if(index == 1) {
					$("#tab3").hide();
					$("#tab4").show();
					$(".addBtnnew").show();
					if(!isLoadIngList) {
						isLoadIngList = true;
						var param = {
							page: ingPage,
							rows: ingRows,
							leaveStatus: '001',
							teacherId: "${user.teacher.id}"
						}
						ingPage += 1;
						loadItem(param, "ingItem");
					}
				}
			})

			$("#approvalOkList").click(function() {
				if(!isLoadApprovalOkList){
					isLoadApprovalOkList = true;
					var param = {
						page: approvalOkPage,
						rows: approvalOkRows,
						leaveStatus: '000',
						approverId: "${user.teacher.id}"
					}
					approvalOkPage += 1;
					loadItem(param, "approvalOkItem");
				}
			})
			
			$("#okList").click(function() {
				if(!isLoadOkList){
					isLoadOkList = true;
					var param = {
						page: okPage,
						rows: okRows,
						leaveStatus: '000',
						teacherId: "${user.teacher.id}"
					}
					okPage += 1;
					loadItem(param, "okItem");
				}
			})
			
			$(".dropload-down").on("click", ".dropload-hasData", function() {
				var id = $(this).closest(".weui_tab_bd_item").find(".weui_panel_bd").attr("id");
				var param = {};
				if(id == 'ingItem') {
					param.page = ingPage;
					param.rows = ingRows;
					param.leaveStatus = '001';
					param.teacherId = "${user.teacher.id}";
					ingPage += 1;
				}else if(id == 'okItem') {
					param.page = okPage;
					param.rows = okRows;
					param.leaveStatus = '000';
					param.teacherId = "${user.teacher.id}";
					okPage += 1;
				}else if(id == 'approvalIngItem') {
					param.page = approvalIngPage;
					param.rows = approvalIngRows;
					param.leaveStatus = '001';
					param.approverId = "${user.teacher.id}";
					approvalIngPage += 1;
				}else if(id == 'approvalOkItem') {
					param.page = approvalOkPage;
					param.rows = approvalOkRows;
					param.leaveStatus = '000';
					param.approverId = "${user.teacher.id}";
					approvalOkPage += 1;
				}
				loadItem(param, id);
			})
			var param = {
				page: approvalIngPage,
				rows: approvalIngRows,
				leaveStatus: '001',
				approverId: "${user.teacher.id}"
			}
			approvalIngPage += 1;
			loadItem(param, "approvalIngItem");
		});
		function loadItem(param, id) {
			$.ajax({
				url: basePath + '/ls/teacher/leave/page/query',
				type: 'post',
				dataType: 'json',
				data: param,
				success: function(data) {
					if(id == "ingItem") {
						$("#ingCount").html(data.data.total);
					}else if(id == "approvalIngItem"){
						$("#approvalIngCount").html(data.data.total);
					}
					if(data.data.rows.length == 0 || param.page*param.rows >= data.data.total) {// 如果没有数据
	            		$("#" + id).closest(".weui_tab_bd_item").find('.loadData').removeClass("dropload-hasData").addClass("dropload-noData").html("没有更多数据了");
					}
					loadHtml(data.data, id, param.teacherId != null ? 1 : 2);
				}
			});
		}
		
		function loadHtml(data, id, type) {
			var myTemplate = Handlebars.compile($("#apply-table-template").html());
			Handlebars.registerHelper("equal", function(v1,v2,options) {
				if(v1 == v2) {
					//满足添加继续执行
					return options.fn(this);
				}else {
					//不满足条件执行{{else}}部分
					return options.inverse(this);
				}
			});
			Handlebars.registerHelper("dateFormat", function(v1) {
				return getTime(v1, "yyyy/MM/dd hh:mm");
			});
			
			Handlebars.registerHelper("statusFormat", function(v1,options) {
				if(v1 == '000') return "<img class='img-max' src='"+basePath+"/images/shenpitongguo.png'>";
				if(v1 == '001') return "<img class='img-max' src='"+basePath+"/images/daish.png'>";
				if(v1 == '002') return "<img class='img-max' src='"+basePath+"/images/shenpibohui.png'>";
				if(v1 == '003') return "已取消";
				return "";
			});
			
			Handlebars.registerHelper("todateFormat", function(v1, v2) {
				return getTime(v1, "MM/dd hh:mm") + "-" + getTime(v2, "MM/dd hh:mm");
			});
			$('#' + id).append(myTemplate(data));
		}
		
		function agreeSubmit(id) {
			$.confirm("确定同意请假吗?", "确认同意?", function() {
				var params = {
		   			id: id,
		   			leaveStatus: '000'
		   		}
		   		auditSubmit(params);
	        }, function() {
	          //取消操作
	        });
		}
		function approvalSubmit(id,type) {
			var id = id;
			var type = type;
			$("#rejectText").attr("placeholder", type == 1 ? "请输入不同意理由" : "请输入审批留言(可不填)");
			$(".weui_mask").addClass("weui_mask_visible").show();
			$(".weui_dialog").addClass("weui_dialog_visible").show();
			$("#rejectCancel").click(function(){
				$("#rejectText").val(null);
				$("#weui_mask").hide();
				$("#weui_dialog").hide();
			});
			$("#rejectSure").click(function(){
				var params = {id: id};
				var text = $("#rejectText").val();
				if(type == 1) {
					if(text.trim().length == 0) return;
					params.rejectReason = text;
		      		params.leaveStatus = '002';
				}else {
					if(text.trim().length > 0) params.message = text;
					params.leaveStatus = '000';
				}
				auditSubmit(params);
			});
		}
		var issubmit = false;
		function auditSubmit(params) {
			if(issubmit) return;
			issubmit = true;
			$.ajax({
	          	url : basePath + "/ls/teacher/leave/audit",
	  	 		type : "post",
	  	 		dataType : "json",
	  	 		data : params,
	            success: function(data){
	            	issubmit = false;
	            	if(data.code == '000') {
	            		$("#weui_mask").hide();
						$("#weui_dialog").hide();
	            		$.toast("审批成功");
	            		setTimeout(function() {
	            			window.location.href = basePath + "/ls/teacher/leave?v=" + new Date().getTime();
	            		}, 1000);
	            		return;
	            	}
	            	$.toptips("提交失败，请联系管理员");
	            },
	            error: function(xhr, type){
	              
	            }
	         });
		}
</script>
</body>
</html>